/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 26
 * Time: 16:13
 */
//牛客 比较版本号
public class Solution2 {
    //法一 官方推荐方法
    public int compare (String version1, String version2) {
        int i = 0;
        int j = 0;
        while (i < version1.length() || j < version2.length()) {//条件用||，因为要两个字符串都遍历结束才能退出循环
            long num1 = 0;
            while(i < version1.length() && version1.charAt(i) != '.') {
                num1 = num1 * 10 + (version1.charAt(i) - '0');//重点
                i++;
            }
            i++;//跳过version1的.
            long num2 = 0;
            while(j < version2.length() && version2.charAt(j) != '.') {
                num2 = num2 * 10 + (version2.charAt(j) - '0');//重点
                j++;
            }
            j++;//跳过version2的.
            if(num1 > num2) return 1;
            if(num1 < num2) return -1;
        }
        return 0;
    }

    //法二 官方方法
    public int compare1 (String version1, String version2) {
        String[] arr1 = version1.split("\\.");
        String[] arr2 = version2.split("\\.");
        int i = 0;
        while(i < arr1.length || i < arr2.length) {
            String string3 = i < arr1.length ? arr1[i] : "0";//因为是赋值给字符串类型的引用，所以0用双引号“”引
            String string4 = i < arr2.length ? arr2[i] : "0";
            long num1 = 0;
            for (int j = 0; j < string3.length(); j++) {
                num1 = num1 * 10 + (string3.charAt(j) - '0');
            }
            long num2 = 0;
            for (int j = 0; j < string4.length(); j++) {
                num2 = num2 * 10 + (string4.charAt(j) - '0');
            }
            i++;
            if(num1 > num2) return 1;
            if(num1 < num2) return -1;
        }
        return 0;
    }

    //法三
    public int compare2 (String version1, String version2) {
        String[] arr1 = version1.split("\\.");
        String[] arr2 = version2.split("\\.");
        int i = 0;
        while(i < arr1.length || i < arr2.length) {//条件用||，因为要两个字符串都遍历结束才能退出循环
            String string3 = i < arr1.length ? arr1[i] : "0";//因为是赋值给字符串类型的引用，所以0用双引号“”引
            String string4 = i < arr2.length ? arr2[i] : "0";
            i++;
            if(Integer.parseInt(string3) > Integer.parseInt(string4)) return 1;
            if(Integer.parseInt(string3) < Integer.parseInt(string4)) return -1;
        }
        return 0;
    }
}
